package cn.ac.iscas.service.queue;

import cn.ac.iscas.domain.source.FlinkSourceColumn;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author LJian
 * @version 1.0
 * @description: TODO
 * @date 2022/7/11 16:21
 */
@Service
public class TableQueueService {
    //<JobId, <ComponentId, <TableName, List<FlinkSourceColumn>>>>
    private Map<String, Map<String, Map<String, List<FlinkSourceColumn>>>> queue = new ConcurrentHashMap<>(32);

//    public void put(String jobId, String key, String value) {
//        synchronized (this.queue) {
//            if (!this.queue.containsKey(jobId)){
//                ConcurrentHashMap<String, String> data = new ConcurrentHashMap<>(32);
//                data.put(key, value);
//                this.queue.put(jobId, data);
//            } else {
//                this.queue.get(jobId).put(key, value);
//            }
//        }
//    }


    public void put(String jobId, String componentId, Map<String, List<FlinkSourceColumn>> tableColumns) {
        synchronized (this.queue) {
            if (!this.queue.containsKey(jobId)){
                ConcurrentHashMap<String, Map<String, List<FlinkSourceColumn>>> data = new ConcurrentHashMap<>(32);
                data.put(componentId, tableColumns);
                this.queue.put(jobId, data);
            } else {
                this.queue.get(jobId).put(componentId, tableColumns);
            }
        }
    }

    public String getTableOrViewName(String jobId, String componentId) {
        synchronized (this.queue) {
            if (this.queue.containsKey(jobId)) {
                Map<String, Map<String, List<FlinkSourceColumn>>> components = this.queue.get(jobId);
                if (components != null && components.get(componentId) != null && !components.get(componentId).isEmpty()) {
                    return components.get(componentId).keySet().toArray()[0].toString();
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }
    }


    public List<FlinkSourceColumn> getColumns(String jobId, String componentId, String tableName) {
        synchronized (this.queue) {
            if (this.queue.containsKey(jobId)) {
                Map<String, Map<String, List<FlinkSourceColumn>>> components = this.queue.get(jobId);
                if (components != null && components.get(componentId) != null && !components.get(componentId).isEmpty()) {
                    Map<String, List<FlinkSourceColumn>> tables = components.get(componentId);
                    if (tables != null && !tables.isEmpty()) {
                        return tables.get(tableName);
                    } else {
                        return null;
                    }
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }
    }
}
